package org.example.controller.actualCombat;

import org.example.entity.Item;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

//解析页面的逻辑
@Component
public class PlanePageProcessor implements PageProcessor {
    @Override
    public void process(Page page) {
// 1.判断是列表页还是详情页
        Html html = page.getHtml();
        List<Selectable> nodes = html.css("#sitebox dl").nodes();
        if (nodes.size() > 0) {
            List<Item> itemList = new ArrayList<>();
            for (Selectable node : nodes) {
                Item item = new Item();
//                取小说的id
                String novelId = node.css("dt a", "href").get();
                if (novelId!=null){
                    String result = novelId.substring(0, novelId.length() - 1);
                    item.setNovelId(result);
                }
//                小说的图片地址
                String img = node.css("dt a img.lazy", "src").get();
                item.setImg(img);
//                小说的分类
                String novelClass = node.css("dt span", "text").get();
                item.setNovelClass(novelClass);
//                小说的名字
                String name = node.css("dd h3 a", "text").get();
                item.setName(name);
//                小说的状态
                String state = node.css("dd.book_other span:nth-child(2)","text").get();
                if (state != null) {
                    item.setState(state.equals("连载") ? 0 : 1);
                }
//                小说的字数
                String wordNumber = node.css("dd.book_other span:nth-child(3)","text").get();
                if (wordNumber != null) {
                    item.setWordNumber(Long.valueOf(wordNumber));
                }

//                小说的简介
                String synopsis = node.css("dd.book_des","text").get();
                if (synopsis != null) {
                    item.setSynopsis(synopsis);
                }
                itemList.add(item);
               String nodes2 = html.css("#pagelink a:nth-child(13)","href").get();

                page.addTargetRequest("http://www.biqugecp.com"+nodes2);
            }
            page.putField("itemList", itemList);
            //        解析首页中所有的链接地址
            List<String> oldUrlList = html.css("#sitebox dl dt a", "href").all();
            List<String> newUrlList = new ArrayList<>();
            for (String s : oldUrlList) {
                String q = "http://www.biqugecp.com" + s;
                newUrlList.add(q);
            }
            page.addTargetRequests(newUrlList);
//            需要进行翻页处理
            Request request = new Request("http://nextpage.com");
//            把当前的url添加到request的附件中
            request.putExtra("url", page.getUrl().get());
            return;
        }
//        解析小说的详情页面
        try {
            parseItemInfo(page);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public Site getSite() {
        return Site.me();
        }

//    商品详情页面解析的业务逻辑
    private void parseItemInfo(Page page) throws ParseException {
//          3.如果是详情页面
        Item item = new Item();
        Html html = page.getHtml();
//        解析html
        String novelId = html.css("#chapterList li:first-child a", "href").get();
        if (novelId!=null){
            String newN = novelId.substring(0, novelId.indexOf("/", novelId.indexOf("/") + 1));
            item.setNovelId(newN);
        }

//           3.1解析商品的详情信息
        String latelyUpdateTime = html.css("#uptime span","text").get();

        if (latelyUpdateTime != null) {
            // String 转换为 LocalDate
            DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate parsedDate2 = LocalDate.parse(latelyUpdateTime, formatter2);
            item.setLatelyUpdateTime(parsedDate2);
        }


//            3.2把详情信息封装成一个商品对象
//            3.3传递给pipeline
        page.putField("item", item);
    }
}
